<?

//Initiate Authenticate
SecurityRedirect ();

//initiate Vars 
define("PRIMARY_KEY","kdcash_id");
$FileName        = "ais_setup_cash.php";
$FileTemplate    = "ais_setup_cash_form.html";
$SubHeaderTitle  = "Setup Cash";
$HTMLFormName    = "ToolForm";
$TableName       = "tblcash";
$FormAction      = $FileName . "?" . AddParam(GetQueryString("QueryString", ""),"form","$HTMLFormName");
$FormEncType     = "application/x-www-form-urlencoded";
//$PMode         = "";

//Initiate Database Table For Insert & Update;
$FormFields    = array("kdcash"=>"","cashnam"=>"","balance"=>"number","coa"=>"","ktrasl"=>"","symbol"=>"");
buildsqlfield ($TableName, $FormFields);
$KTRASL = GetOfficeID();

//Leave Here Untouch
$FormCName     = BuildHTMLField($FormFields,"name",1);
$FormCValue    = BuildHTMLField($FormFields,"value",1);


//************ FORM ACTION **************
if (GetParam("form","")==$HTMLFormName) {
	if (GetParam("Action","")=="Add") {
		$FormMode="Edit";
		FormInsert();
	}
	if (GetParam("Action","")=="Update") {
		$FormMode="Edit";
		FormUpdate();
	}
	if (GetParam("Action","")=="Delete") {
		$FormMode="Edit";
		FormDelete();
	}
	if (GetParam("Action","")=="Cancel") {
		$FormMode="Edit";
		FormCancel();
	}
}

//Field Verification 
$Verification  = "Enable";
$SessionVar    = md5(microtime());
//$SessionVar    = "Stuck";
$VerifyFields  = array("kdcash"=>"Cash Code","cashnam"=>"Cash Name","balance"=>"Balance","symbol"=>"Symbol");
VerifyHTML ($HTMLFormName,$VerifyFields,$SessionVar);

//************ SET FORM MODE ***************

if (GetParam(PRIMARY_KEY,"")) $FormMode = "Edit";
else $FormMode = "Add";
$InitFunction = "isExist".PRIMARY_KEY."();";
$Initiate .= "Masking();";
$Masking = BuildMasking ($HTMLFormName,array("balance"=>"number"));
$Verify = CreateVerifyLoader ($FileName,"iLoader",PRIMARY_KEY,"$HTMLFormName.".PRIMARY_KEY, $FormMode);
$JSMasks  = $Masking.$Verify;

Init($SessionVar,$JSMasks);


//******* DATABASE FORM QUERY ***********
$SQL = "SELECT * FROM `$TableName` ";
if (GetParam(PRIMARY_KEY,"")) {
    $SQLForm = $SQL."WHERE ".PRIMARY_KEY."='".GetParam(PRIMARY_KEY,"")."'";
} else {  
  $KTRASL = GetOfficeID();
}

//******* SHOW FORM CONTENT *********
$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);

$TBS->MergeBlock("bln",$FormCName);
//HTMLGrid
$SQLCOA = GetCOAParam("KAS",0);
$TBS->MergeBlock("coablk",$DBMysql,$SQLCOA);

if (GetParam(PRIMARY_KEY,"")) $TBS->MergeBlock("blv","mysql",$SQLForm);
else $TBS->MergeBlock("blv",$FormCValue);
//HTML Name
$TBS->MergeBlock("blg",$DBMysql,$SQL);
$TBS->Show();


//*************** ACTION FUNCTION *******************
function FormInsert() {
	global $DBConnection,$FileName;
	$DBConnection->Insert();
	header("Location: $FileName?PageMode=Form");
	exit;
}

function FormUpdate() {
	global $DBConnection;
	$DBConnection->Update();
}

function FormDelete() {
	global $DBConnection,$FileName,$TableName;
	$DBConnection->Delete("$TableName",PRIMARY_KEY);
	header("Location: $FileName");
	exit;	
	
}

function FormSearch () {

}

function FormCancel () {
	global $FileName;
	header("Location: ".$FileName);
	exit;
}
//*************** USER FUNCTION *******************
function buildsqlfield ($table, $arrayfield) {
		global $DBConnection;
		
		foreach ($arrayfield as $key=>$formatting) {
			if ($formatting=="number") {
				$fieldvalue = StrToNumber (GetParam("$key",""));	
			} else $fieldvalue = GetParam("$key","");
			$DBConnection->add_htmlfield($table,$key,$fieldvalue);
		}
}

?>